package org.bing.engine.helper {
    import flash.system.System;
    
    import org.bing.flex.action.ActionFactory;
    import org.bing.flex.action.GeneralAction;
    import org.bing.flex.error.ErrorDialog;

    public class SystemManager {
        private static var action:GeneralAction=ActionFactory.getAction("console.SystemManager");
        private static var serverTime:Number;
        private static var clientTime:Number;
        private static var errorDialog:ErrorDialog = new ErrorDialog();

        public static function init():void {
            // init global error dialog
            getErrorDialog();
            // init system time
            action.invoke("getSystemTime", null, function(data:Object):void {
                serverTime=data as Number;
                clientTime=new Date().time;
            }, function(data:Object):void {
                SystemManager.init();
            });
        }

        public static function getErrorDialog():ErrorDialog {
            if (errorDialog == null) {
                errorDialog=new ErrorDialog();
            }
            return errorDialog;
        }

        public static function getSystemDate():Date {
            return new Date(serverTime + (new Date().time - clientTime));
        }

        public static function getSystemTime():Number {
            return serverTime + new Date().time - clientTime;
        }
    }
}